package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ScanCodeView2
* */
class ScanCodeView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            strokeJoin = Paint.Join.MITER
            strokeWidth = context.resources.getDimension(R.dimen.dp_3)
            color = context.resources.getColor(R.color.color_00E676)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            strokeJoin = Paint.Join.ROUND
            strokeWidth = context.resources.getDimension(R.dimen.dp_1_5)
            color = context.resources.getColor(R.color.color_00E676)
            style = Paint.Style.STROKE
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth / 2.0F
            val cy = mHeight / 2.0F
            val length = cx / 3
            translate(cx, cy)
            mPath0.apply {
                moveTo(-cx, -cy + length)
                lineTo(-cx, -cy)
                lineTo(-cx + length, -cy)
            }
            drawPath(mPath0, mPaint0)

            mPath0.reset()
            mPath0.apply {
                moveTo(cx - length, -cy)
                lineTo(cx, -cy)
                lineTo(cx, -cy + length)
            }
            drawPath(mPath0, mPaint0)

            mPath0.reset()
            mPath0.apply {
                moveTo(cx, cy - length)
                lineTo(cx, cy)
                lineTo(cx - length, cy)
            }
            drawPath(mPath0, mPaint0)

            mPath0.reset()
            mPath0.apply {
                moveTo(-cx + length, cy)
                lineTo(-cx, cy)
                lineTo(-cx, cy - length)
            }
            drawPath(mPath0, mPaint0)

            save()
            translate(0F,length * Math.sin(Math.toRadians(45.0)).toFloat() / 2)
            rotate(42F)
            mPath1.apply {
                moveTo(-length, 0F)
                lineTo(0F, 0F)
                lineTo(0F,-cy * 2 / 3)
            }
            drawPath(mPath1, mPaint1)
            restore()
        }
    }
}